package cn.mumzone.pos.util;

import android.content.Context;

import com.minipos.device.SDK;
import com.minipos.device.SerialPort;
import com.minipos.device.SerialPortConfiguration;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;

/**
 * Created by qiuzhenhuan on 2019/2/12.
 */

public class LedDisplayUtil {

    /*
     * 用串口发送指令
     */
    private SerialPort mSerialPort;


    /**
     * 客显类型  led / 其他
     */
    private String mCustDspType;

    /**
     * 端口
     */
    private String mLedPort;

    private Context mContext;

    /**
     * @throws Throwable
     */
    public LedDisplayUtil(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        if (AppUtil.isBaoYingPos()) {
            try {
                SDK.init(mContext);
                loadSystemProperties();
                SerialPortConfiguration cfg = new SerialPortConfiguration();
                cfg.port = mLedPort;
                cfg.baudrate = 2400;
                cfg.databits = SerialPort.DATABITS_8;
                cfg.stopbits = SerialPort.STOPBITS_1;
                cfg.parity = SerialPort.PARITY_NONE;
                cfg.flowControl = SerialPort.FLOWCONTROL_NONE;
                mSerialPort = SerialPort.open(cfg, true);

            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

    }

    private void loadSystemProperties() {
        Properties p = new Properties();
        FileInputStream is = null;

        try {
            is = new FileInputStream("/system/build.prop");
            p.load(is);
            mCustDspType = p.getProperty("ro.customerdisplay.type", "lcd");
            mLedPort = p.getProperty("ro.customerdisplay.port", "/dev/ttyACM0");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 关闭客显（串口）
     * 用完都有记得关闭，否则会造成系统资源浪费
     */
    public void close() {
        mSerialPort.close();
    }

    /**
     * 清屏
     *
     * @throws IOException
     */
    public void clear() {
        if (!AppUtil.isBaoYingPos()) {
            return;
        }
        try {
            if (mSerialPort != null)
                mSerialPort.getOutputStream().write(0x0C);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复位
     *
     * @throws IOException
     */
    public void reset() {
        try {
            byte[] cmd = {0x1B, 0x40};
            mSerialPort.getOutputStream().write(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示单价
     *
     * @param value
     * @throws IOException
     */
    public void displayPrice(String value) {
        displayValue(value, 0x31);
    }

    /**
     * 显示总计
     *
     * @param value
     * @throws IOException
     */
    public void displayTotal(String value) {
        displayValue(value, 0x32);
    }

    /**
     * 显示付款
     *
     * @param value
     * @throws IOException
     */
    public void displayPayment(String value) {
        displayValue(value, 0x33);
    }

    /**
     * 显示找零
     *
     * @param value
     * @throws IOException
     */
    public void displayChange(String value) {
        displayValue(value, 0x34);
    }

    private void displayValue(String value, int type) {

        if (!AppUtil.isBaoYingPos() || StringUtils.isEmpty(mCustDspType) || !mCustDspType.equals("led") || mSerialPort == null) {
            return;
        }
        try {
            String number = BigDecimalUtil.getNumber(value);
            number += "\r";
            mSerialPort.getOutputStream().write(number.getBytes());


            byte[] cmd = {0x1B, 0x73, (byte) type};
            mSerialPort.getOutputStream().write(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
